package oboe.controllers.jml.helpers.list;

import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

import oboe.controllers.jml.events.JMLEvent;
import oboe.controllers.jml.helpers.JMLClipper;

import flash.Vector;

class TextList extends JMLClipper
{
private var items:Vector<TextField>;
private var nextY:Float;
public var fillColor:Int = 0xEFEFEF;
public var linePadding:Int = 5;

public function new()
{
	super();
	this.items = new Vector();
	this.nextY = 0;
	Object(this).addCallback( JMLEvent.JMLCOMPLETE, redraw );
	Object(this).addCallback( "changeSize", redraw );
}

private function redraw( ):Void
{
	this.graphics.clear();
	this.graphics.beginFill( fillColor, 1 );
	this.graphics.drawRect( 0, 0, this.width, this.height );
	this.graphics.endFill();
}

public function getItemCount():Int
{
	return this.items.length;
}

public function pushItem( str:String ):Int
{
	var tf:TextField = new TextField();
	items.push( tf );
	tf.autoSize = TextFieldAutoSize.LEFT;
	tf.text = str;
	tf.y = nextY;
	this.addChild( tf );
	nextY += tf.height + linePadding;
	this.dispatchEvent( new Event("changeSize") );
	return items.length - 1;
}

public function popItem():Void
{
	var tf:TextField = items.pop();
	this.nextY -= tf.height + linePadding;
	this.removeChild( tf );
	this.dispatchEvent( new Event("changeSize") );
}

public function clear():Void
{
	for each( var item:TextField in this.items )
	{
	this.removeChild( item );
	}
	
	this.items.length = 0;
	this.nextY = 0;
	this.dispatchEvent( new Event("changeSize") );
}

public function setItem( i:Int, val:String ):Void
{
	this.items[ i ].text = val;
}

public function getItem( i:Int ):String
{
	return this.items[ i ].text;
}
}